package com.yjz.study.adapter.home

import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.yjz.common.extend.loadCorner
import com.yjz.log.util.DisplayUtil
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.http.entity.res.HomeBanner
import com.yjz.ui.banner.BannerLayout
import com.yjz.ui.banner.core.BannerAdapter
import com.yjz.ui.banner.core.BannerModel
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder

/**
 *作用 : 首页顶部轮博图
 *作者 : yjz
 *时间 : 2025/3/14 08:53
 */
class BannerItem(var bannerList: List<HomeBanner>) : HiDataItem<List<HomeBanner>, HiViewHolder>(bannerList) {

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val banner = holder.itemView as BannerLayout
        val mutableListOf = mutableListOf<BannerModel>()
        bannerList.forEachIndexed { index, homeBanner ->
            mutableListOf.add(BannerModel(homeBanner.cover))
        }
        banner.setOnBannerClickListener { _, _, position ->
            val homeBanner = bannerList[position]
            if (TextUtils.equals(homeBanner.type, HomeBanner.TYPE_GOODS)) {
                Toast.makeText(holder.itemView.context, "you  touch me:$position", Toast.LENGTH_SHORT).show()
            } else {
                ARouterLogic.startActivity4Browser(homeBanner.url)
            }
        }
        banner.setBindAdapter { viewHolder: BannerAdapter.ViewHolder, mo: BannerModel, position: Int ->
            val imageView = viewHolder.rootView as ImageView
            imageView.loadCorner(mo.url, DisplayUtil.dp2px(5f))
        }
        banner.setBannerData(mutableListOf)
    }

    override fun getItemView(parent: ViewGroup): View? {
        val banner = BannerLayout(parent.context)
        banner.setIntervalTime(2000)
        val params = RecyclerView.LayoutParams(
            RecyclerView.LayoutParams.MATCH_PARENT,
            DisplayUtil.dp2px(160f)
        )
        val margin = DisplayUtil.dp2px(10f)
        params.setMargins(margin, margin, margin, margin)
        banner.layoutParams = params
        banner.setBackgroundColor(Color.WHITE)
        return banner
    }
}